
import { ConfigProvider, message } from '@/components/base';
import { useObserver } from 'mobx-react-lite'
import zhCN from 'antd/locale/zh_CN';
import { themeConfig } from '@/constant/theme-config';
import stores from '@/stores';
import { Route, Routes } from 'react-router-dom';
import * as Views from '@/views';
import request from "@shared/apis/request";

export default function App() {
  const token = useObserver(() => stores.token);
  return (
    <ConfigProvider locale={zhCN} theme={stores?.themeConfig?.theme || themeConfig.get('dsff-default')?.theme} >
      {token
        ? (
          <Routes>
            <Route path='*' element={<Views.BasicLayout />} />
          </Routes>
        ) : (
          <Routes>
            <Route path='*' element={<Views.Logon />} />
          </Routes>
        )}
    </ConfigProvider>
  )
}

request.interceptors.request.use(
  (config) => {
    if (stores.token) {
      config.headers.Authorization = `Bearer ${stores.token}`
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
)

request.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    // 检查是否设置了 silent 参数
    if (!error.config?.silent) {
      // 如果是取消请求，不显示错误提示
      if (error.name === 'CanceledError' || error.code === 'ERR_CANCELED') {
        return Promise.reject(error)
      }
      const msg = error.response?.data?.message || '请求失败';
      message.error({ content: msg, key: msg });
    }
    return Promise.reject(error)
  }
)